using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Dapper.Contrib.Extensions;
using Read.Models;
using Read.Models.Models;
using Read.Utility;

namespace Read.Services
{
    /// <summary>
    /// 日志服务
    /// </summary>
    public class LogService : BaseService
    {
        /// <summary>
        /// 查询登录日志
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultData<List<LoginLogModel>> getLoginLogList(QueryParamsLoginLog query)
        {
            string countQuery = $"SELECT COUNT(1) FROM `{Tables.LoginLog}`  /**where**/";
            string sql = $"SELECT * FROM `{Tables.LoginLog}` /**where**/ /**orderby**/ limit @PageIndex,@PageSize";

            if (query.TimeMin != null)
            {
                string[] datestrs = query.TimeMin.Split(new Char[] { '~' });
                query.TimeMin = Tools.ToUnixTime(Convert.ToDateTime(datestrs[0].Trim())).ToString();
                query.TimeMax = Tools.ToUnixTime(Convert.ToDateTime(datestrs[1].Trim())).ToString();
            }
            var currentPageIndex = (query.Index - 1) * query.Size;
            SqlBuilder builder = new SqlBuilder();

            var count = builder.AddTemplate(countQuery);
            var selector = builder.AddTemplate(sql, new { PageIndex = currentPageIndex, PageSize = query.Size });

            if (!string.IsNullOrEmpty(query.TimeMin))
                builder.Where("Time >= @TimeMin", new { TimeMin = query.TimeMin });

            if (!string.IsNullOrEmpty(query.TimeMax))
                builder.Where("Time < @TimeMax", new { TimeMax = query.TimeMax });

            //排序
            builder.OrderBy("Id DESC");
            //Console.WriteLine("=====" + selector.RawSql);
            //Console.WriteLine("-----" + selector.Parameters.ToJson());
            var totalRecords = connection.Query<int>(count.RawSql, count.Parameters).Single();
            var value = connection.Query<LoginLogModel>(selector.RawSql, selector.Parameters).ToList();
            var res = new ResultData<List<LoginLogModel>>()
            {
                Result = true,
                Total = totalRecords,
                Data = value,
                Message = "查询成功"
            };
            return res;
        }

        /// <summary>
        /// 操作日志列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ResultData<List<OperationLogModel>> getOperationLogList(QueryParamsOperationLog query)
        {
            string countQuery = $"SELECT COUNT(1) FROM `{Tables.OperationLog}`  /**where**/";
            string sql = $"SELECT * FROM `{Tables.OperationLog}` /**where**/ /**orderby**/ limit @PageIndex,@PageSize";

            if (query.TimeMin != null)
            {
                string[] datestrs = query.TimeMin.Split(new Char[] { '~' });
                query.TimeMin = Tools.ToUnixTime(Convert.ToDateTime(datestrs[0].Trim())).ToString();
                query.TimeMax = Tools.ToUnixTime(Convert.ToDateTime(datestrs[1].Trim())).ToString();
            }
            var currentPageIndex = (query.Index - 1) * query.Size;
            SqlBuilder builder = new SqlBuilder();

            var count = builder.AddTemplate(countQuery);
            var selector = builder.AddTemplate(sql, new { PageIndex = currentPageIndex, PageSize = query.Size });

            if (!string.IsNullOrEmpty(query.TimeMin))
                builder.Where("Time >= @TimeMin", new { TimeMin = query.TimeMin });

            if (!string.IsNullOrEmpty(query.TimeMax))
                builder.Where("Time < @TimeMax", new { TimeMax = query.TimeMax });

            //排序
            builder.OrderBy("Id DESC");
            //Console.WriteLine("=====" + selector.RawSql);
            //Console.WriteLine("-----" + selector.Parameters.ToJson());
            var totalRecords = connection.Query<int>(count.RawSql, count.Parameters).Single();
            var value = connection.Query<OperationLogModel>(selector.RawSql, selector.Parameters).ToList();
            var res = new ResultData<List<OperationLogModel>>()
            {
                Result = true,
                Total = totalRecords,
                Data = value,
                Message = "查询成功"
            };
            return res;
        }
        /// <summary>
        /// 登录日志添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long loginLogAdd(LoginLogModel model)
        {
            return connection.Insert(model);
        }
        /// <summary>
        /// 操作日志添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long operationLogAdd(OperationLogModel model)
        {
            return connection.Insert(model);
        }
    }
}